class Solution {
public:
    int minimumDistance(vector<int>& nums) {
        unordered_map<int, vector<int>> groups;
        for (int i = 0; i < nums.size(); i++) {
            groups[nums[i]].push_back(i);
        }

        int minDist = INT_MAX;

        for (auto& [num, indices] : groups) {
            if (indices.size() < 3) continue;

            for (int i = 0; i <= indices.size() - 3; i++) {
                int dist = 2 * (indices[i + 2] - indices[i]);
                minDist = min(minDist, dist);
            }
        }

        return minDist == INT_MAX ? -1 : minDist;
    }
};